package zozopo.comm;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class CNSpell {
	public static HanyuPinyinToneType[] toneTypes = new HanyuPinyinToneType[] {
		   HanyuPinyinToneType.WITH_TONE_NUMBER,
		   HanyuPinyinToneType.WITHOUT_TONE,
		   HanyuPinyinToneType.WITH_TONE_MARK };
		 public static HanyuPinyinVCharType[] vCharTypes = new HanyuPinyinVCharType[] {
		   HanyuPinyinVCharType.WITH_U_AND_COLON, HanyuPinyinVCharType.WITH_V,
		   HanyuPinyinVCharType.WITH_U_UNICODE };
		 public static HanyuPinyinCaseType[] caseTypes = new HanyuPinyinCaseType[] {
		   HanyuPinyinCaseType.LOWERCASE, HanyuPinyinCaseType.UPPERCASE };

		 /**
		  * 将中文字符转换为相应的数组
		  * 
		  * @param chineseCharacter
		  * @param toneSelection
		  * @param vcharSelection
		  * @param caseSelection
		  */
		 private static String updateFormattedText(String chineseCharacter,
		   HanyuPinyinToneType toneSelection,
		   HanyuPinyinVCharType vcharSelection,
		   HanyuPinyinCaseType caseSelection) {
			 
			 StringBuffer pinyinStrBuf = new StringBuffer();
		  // 拼音格式化对象
		  HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
		  // 设置中文声调
		  if (toneSelection != null) {
		   if (toneTypes[0] == toneSelection) {
		    outputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_NUMBER);
		   } else if (toneTypes[1] == toneSelection) {
		    outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		   } else if (toneTypes[2] == toneSelection) {
		    outputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
		   }
		  }
		  // 设置特殊拼音的转换
		  if (vcharSelection != null) {
		   if (vCharTypes[0] == vcharSelection) {
		    outputFormat
		      .setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);
		   } else if (vCharTypes[1] == vcharSelection) {
		    outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
		   } else if (vCharTypes[2] == vcharSelection) {
		    outputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
		   }
		  }
		  // 设置转换的拼音的大小写
		  if (caseSelection != null) {
		   if (caseTypes[0] == caseSelection) {
		    outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		   } else if (caseTypes[1] == caseSelection) {
		    outputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
		   }
		  }
		  // 获取字符串
		  char[] cnStr = chineseCharacter.toCharArray();
		  for (char chineseChar : cnStr) {
		   // 将字符转换为拼音数组
		   String[] pinyinArray = null;
		   try {
		    pinyinArray = PinyinHelper.toHanyuPinyinStringArray(
		      chineseChar, outputFormat);
		   } catch (BadHanyuPinyinOutputFormatCombination e1) {
		    e1.printStackTrace();
		   }
		   String outputString = concatPinyinStringArray(pinyinArray);
		   pinyinStrBuf.append(outputString);
		   //System.out.println(outputString);
		  }
		  return pinyinStrBuf.toString();
		 }

		 /**
		  * 中文转换拼音方法
		  * @author shea
		  */
		 public static String CNToSpell(String str) {
		  return updateFormattedText(str, HanyuPinyinToneType.WITHOUT_TONE,
		    HanyuPinyinVCharType.WITH_U_UNICODE,
		    HanyuPinyinCaseType.LOWERCASE);
		 }
		 
		 /**
		  * 取汉字的首字母 
		  * @author shea
		  */
		 public static String CNToFirstWordSpell(String str) {
			 String firstWordSpell = "";
			 char[] arryStr = str.toCharArray();
			 
			 for(char c:arryStr){ 
				//获取所有的拼音   
			      String []pinyingStr=PinyinHelper.toHanyuPinyinStringArray(c);
			      if(pinyingStr == null){
			    	  firstWordSpell += c+"";
			      }else{
			    	  firstWordSpell += pinyingStr[0].substring(0, 1);
			      }
				  
			 }
		   
		  return firstWordSpell;
		 }

		 /**
		  * 将转换的拼音数组转换为字符串
		  * 
		  * @param pinyinArray
		  * @return
		  */
		 private static String concatPinyinStringArray(String[] pinyinArray) {
		  StringBuffer pinyinStrBuf = new StringBuffer();

		  if ((null != pinyinArray) && (pinyinArray.length > 0)) {
		   //for (int i = 0; i < pinyinArray.length; i++) {
		    pinyinStrBuf.append(pinyinArray[0]);
		    //pinyinStrBuf.append(System.getProperty("line.separator"));
		  // }
		  }
		  String outputString = pinyinStrBuf.toString();
		  return outputString;
		 }
		 
		 /**  
		     * 取汉字的首字母  
		     * @param src  
		     * @param isCapital 是否是大写  
		     * @return  
		     */  
		    public static char[]  getHeadByChar(char src,boolean isCapital){   
		        //如果不是汉字直接返回   
		        if (src <= 128) {   
		            return new char[]{src};
		        }   
		        //获取所有的拼音   
		        String []pinyingStr=PinyinHelper.toHanyuPinyinStringArray(src);   
		        //创建返回对象   
		        int polyphoneSize=pinyingStr.length;   
		        char [] headChars=new char[polyphoneSize];   
		        int i=0;   
		        //截取首字符   
		        for(String s:pinyingStr){   
		            char headChar=s.charAt(0);   
		            //首字母是否大写，默认是小写   
		            if(isCapital){   
		                headChars[i]=Character.toUpperCase(headChar);   
		             }else{   
		                headChars[i]=headChar;   
		             }   
		            i++;   
		        }   
		           
		        return headChars;   
		    }   

}
